
'''
# 捕获异常, 程序可以继续执行
try:
    print("-----test-----1-----")
    f = open("123.txt", "r")
    print("-----test-----2-----")

except IOError:  # 文件没找到属于 IOError (输入输出异常)
    pass
'''


'''
try:
    print(num)
except NameError:  # 异常类型捕获, 类型需要一致
    print("产生错误了")
'''


'''
try:
    print("-----test-----1-----")
    f = open("123.txt", "r")
    print("-----test-----2-----")

    print(num)

# except (NameError, IOError):
except Exception:         # 捕获所有的异常类型
    print("产生错误了")
'''

'''
# 获取异常描述
try:
    print("-----test-----1-----")
    f = open("123.txt", "r")
    print("-----test-----2-----")

    print(num)

# except (NameError, IOError) as result:         # 捕获所有的异常类型
except Exception as result:
    print("产生错误了")
    print(result)
'''



# try catch finally 嵌套

import time

try:
    f = open("test.txt", "r")
    try:
        while True:
            content = f.readline()
            if len(content) == 0:
                break
            time.sleep(2)
            print(content)
    finally:
             f.close()
             print("文件关闭")

except Exception as result:
    print("发生异常了")